package com.itheima.health.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.common.ValidateCodeType;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.DuplicateOperateException;
import com.itheima.health.exception.OrderCountOverLimitExceptoin;
import com.itheima.health.exception.OrderTimeNotAllowedException;
import com.itheima.health.mobile.vo.OrderSubmitParam;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mobile/order")
@Slf4j
public class OrderController {
    @Reference
    private SmsService smsService;
    @Reference
    private OrderService orderService;
    @Reference
    private MemberService memberService;
    @Reference
    private SetMealService setMealService;

    @RequestMapping("/submit")
    public Result submit(@RequestBody OrderSubmitParam param){
        log.info("[预约设置-提交]data:{}",param);
        //1.验证码验证 SmsService
        if (!smsService.checkValidateCode(ValidateCodeType.ORDER,param.getTelephone(),param.getValidateCode())){
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        //2.创建会员 MemberService
        Member member = new Member();
        member.setRegTime(new Date());
        member.setIdCard(param.getIdCard());
        member.setSex(param.getSex()==1?"男":"女");
        member.setPhoneNumber(param.getTelephone());
        member.setName(param.getName());
        try {
            member=memberService.add(member);
        } catch (DuplicateOperateException e) {
            log.info("",e);
            member=memberService.findByPhoneNumber(member.getPhoneNumber());
        }
        //3.提交体检信息 OrderService
        Order order=new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(param.getOrderDate());
        order.setOrderStatus(order.getOrderStatus());
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setSetmealId(param.getSetMealId());
        try {
            order=orderService.add(order);
        } catch (OrderCountOverLimitExceptoin e) {
            log.info("",e);
            return new Result(false,MessageConst.HAS_ORDERED);
        } catch (DuplicateOperateException e) {
            log.info("",e);
            return new Result(false,MessageConst.ORDER_FULL);
        } catch (OrderTimeNotAllowedException e) {
            log.info("",e);
            return new Result(false,MessageConst.SELECTED_DATE_CANNOT_ORDER);
        }

        //4.返回结果

        return new Result(true,MessageConst.ORDER_SUCCESS,order);
    }

    /**
     * <p>体检人：{{orderInfo.member}}</p>
     * <p>体检套餐：{{orderInfo.setmeal}}</p>
     * <p>体检日期：{{orderInfo.orderDate}}</p>
     * <p>预约类型：{{orderInfo.orderType}}</p>
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        try {
            //order
            Order order=orderService.findById(id);
            if (null == order){
                return new Result(false,"数据不存在");
            }
            //member
            Member member=memberService.findById(order.getMemberId());

            //setmeal
            Setmeal setmeal=setMealService.findById(order.getSetmealId());
            Map<String , Object> map=new HashMap<>();
            map.put("member",member.getName());
            map.put("setmeal",setmeal.getName());
            map.put("orderDate",new SimpleDateFormat("yyyy-MM-dd").format(order.getOrderDate()));
            map.put("orderType",order.getOrderType());

            return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,map);
        } catch (RuntimeException e) {
            log.error("",e);
            return new Result(false,MessageConst.QUERY_ORDER_FAIL);
        }
    }
}
